package com.deng.maintenance.kpi.view
{
	import com.deng.common.view.CommonDG;
	import com.deng.maintenance.kpi.vo.KpiDictItemVsOrganizationVo;
	import com.deng.maintenance.model.MaintenanceModelLocator;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	public class KpiSelectCB  extends CheckBox
	{
		private var pvo:KpiDictItemVsOrganizationVo;
		public function KpiSelectCB()
		{
			this.addEventListener(FlexEvent.CREATION_COMPLETE,showHandler);
			this.height = 10;
//			this.addEventListener(Event.CHANGE,showHandler);
			this.addEventListener(FlexEvent.SHOW,showHandler);//本来是2
			this.addEventListener(MouseEvent.CLICK,updateEvt);
			this.addEventListener(KeyboardEvent.KEY_DOWN,kdEvt);
			super();
			
		}
		private function kdEvt(event:KeyboardEvent)
		{
			if(event.keyCode == 32)
			{
				event.stopImmediatePropagation();
				return;
			}
		}
		private function updateEvt(event:MouseEvent):void
		{
			if(this.selected){//之前选中的，后面要删除
				if(pvo.detail_type == 2 && pvo.detail_id == 0)//父
				{//说明要删除全部子
					this.selected = false;
					var id:int = pvo.id;
					var ar:Array = CommonDG(listData.owner).getLI();
					for(var i:int = 0;i < ar.length;i++)
					{
						if(ar[i] == null || (ar[i] as Array).length == 0)
						{
							break;
						}
						var ob:KpiDictItemVsOrganizationVo = ar[i][0].data as KpiDictItemVsOrganizationVo;
						if(ob.id == id)
						{
							CheckBox(ar[i][1]).selected = false;
							(MaintenanceModelLocator.getInstance().kpiDictItemVsOrganizationList.getItemAt(i) as KpiDictItemVsOrganizationVo).guikou_name = null;
							(MaintenanceModelLocator.getInstance().kpiDictItemVsOrganizationList.getItemAt(i) as KpiDictItemVsOrganizationVo).guikou_id = null;
						}
					}
				}
				var ro:RemoteObject = new RemoteObject();
				ro.destination = "KPIManager";
				ro.addEventListener(ResultEvent.RESULT,delRst);
				ro.addEventListener(FaultEvent.FAULT,flt);
				ro.deleteKpiVsKaohe(pvo.id,pvo.kaohe_id,pvo.detail_id);
//				Alert.show("确定要删除么？", "删除选项", (Alert.YES| Alert.NO) , 
//					this.parent.parent.parent as Canvas, 
//					deleteAlert);					
			}else{//之前没选中，后面要增加
//			Alert.show(this.parent+"   "+this.parent.parent+"   "+this.parent.parent.parent);
				var bbp:KpiSelect2Guikou = KpiSelect2Guikou(PopUpManager.createPopUp(this.parent.parent.parent.parent, KpiSelect2Guikou , true));
				bbp.setParentVO(pvo,this);
				PopUpManager.centerPopUp(bbp);
			}			
		}

		private function flt(ft:FaultEvent):void
		{
			Alert.show(ft.fault.getStackTrace());
		}
		private function deleteAlert(event:CloseEvent):void
		{
			if (event.detail==Alert.YES)
		 	{		 		
		 		var ro:RemoteObject = new RemoteObject();
				ro.destination = "KPIManager";
				ro.addEventListener(ResultEvent.RESULT,delRst);
				ro.addEventListener(FaultEvent.FAULT,flt);
				ro.deleteKpiVsKaohe(pvo.id,pvo.kaohe_id,pvo.detail_id);
		 	}		      
		    else
		      this.selected = true;
		}
		private function delRst(rst:ResultEvent):void
		{
			Alert.show("删除成功");
			pvo.guikou_name = null;
			pvo.guikou_id = null;
			this.selected = false;
//			var rtEvt1:ReturnEvent=new ReturnEvent(ReturnEvent.searchKpiByKaohe);
//			rtEvt1.dispatch();
		}
		private function showHandler(event:FlexEvent):void
		{
			
//			if(this.data is KpiDictItemVsOrganizationVo){
				pvo = this.data as KpiDictItemVsOrganizationVo;
//			}else{
//				Alert.show("KpiDictItemVsOrganizationVo???");
//			}
//			if(pvo.detail_type == 2 && pvo.detail_id == 0)//明细的父
//			{
//				this.visible = false;
//			}
//			else
//			{
//				this.visible = true;
//			}
			if(pvo.detail_type == 2 && pvo.detail_id != 0)
			{
				this.setStyle("textAlign","right");
			}
			else
			{
				this.setStyle("textAlign","center");
			}
			
			
			if(pvo.guikou_id != 0){
				this.selected = true;
			}else{
				this.selected = false;
			}
			
		}

	}
}